/**
 * 2016，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	AUserConverter.java
 * Created：		2016年7月30日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.user.converter;

import java.util.ArrayList;

import org.springframework.core.convert.converter.Converter;

import com.thor.commons.entity.KV;
import com.thor.commons.util.ConverterUtil;
import com.thor.palmmall.rest.user.AUser;
import com.thor.palmmall.spi.user.SUser;

/**
 * @author wolv
 *
 */
public class AUserConverter implements Converter<SUser, AUser> {

  public static AUserConverter getInstance() {
    if (instance == null)
      instance = new AUserConverter();
    return instance;
  }

  private static AUserConverter instance;

  private AUserConverter() {
  }

  @Override
  public AUser convert(SUser source) {
    if (source == null)
      return null;

    AUser target = new AUser();
    target.setCode(source.getCode());
    target.setName(source.getName());
    target.setTelephone(source.getTelephone());
    target.setOrgCaption(source.getOrgCaption());
    target.setPermissions(new ArrayList<String>());
    target.getPermissions().addAll(source.getPermissions());
    target.setStores(ConverterUtil.convert(source.getStores(), AStoreConverter.getInstance()));
    target.setUuid(source.getUuid());
    target.setUserGroups(new ArrayList<KV>());
    if (source.getUserGroups() != null)
      for (KV sourceUserGroup : source.getUserGroups())
        target.getUserGroups().add(KV.newInstance(sourceUserGroup));

    target.setPermObjects(
        ConverterUtil.convert(source.getPermObjects(), APermObjectsConverter.getInstance()));

    return target;
  }
}
